package com.ssm.controller;

import com.ssm.entity.Admin;
import com.ssm.entity.Student;
import com.ssm.entity.Teacher;
import com.ssm.service.AdminService;
import com.ssm.service.StudentService;
import com.ssm.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Lenovo on 2019-10-3.
 */
@Controller
public class LoginController {
    @Autowired
    private StudentService studentServiceImpl;
    @Autowired
    private TeacherService teacherServiceImpl;
    @Autowired
    private AdminService adminServiceImpl;

    @RequestMapping("check.do")
    public String check(String username, String password, String type, HttpServletRequest request) throws DatabaseException {
        switch (Integer.parseInt(type)) {
            case 0: {
                Student student = studentServiceImpl.login(username, password);
                if (student != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute("username", username);
                    return "redirect:/Student/list.do";
                } else {
                    return "login";
                }
            }
            case 1: {
                Teacher teacher = teacherServiceImpl.login(username, password);
                if (teacher != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute("username", username);
                    return "redirect:/Teacher/list.do";
                } else {
                    return "login";
                }
            }
            case 2: {
                Admin admin = adminServiceImpl.login(username, password);
                if (admin != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute("username", username);
                    return "redirect:/Admin/list.do";
                } else {
                    return "login";
                }
            }
            default:
            {
            return "login";
            }
        }
    }


}
